<?php
/**
 * 校园卡服务层
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/4/1
 * Time: 12:54
 */

class CampusCardService extends BaseService
{
    /**
     * 校园卡支付
     *
     */
    public function campusCardPay($account_id,$user_id,$order,$thirdurl, $errorurl,$pay_type, $goodsNameCampusCard = '',$account)
    {
        if (!$order) {
            return array('status' => 'F', 'message' => '订单信息不能为空');
        }
        if (!$thirdurl) {
            return array('status' => 'F', 'message' => '跳转地址不能为空');
        }
        if (!$errorurl) {
            return array('status' => 'F', 'message' => '错误返回地址不能为空');
        }
        $campuscard = M("campuscard")->where(array("account_id" => $account_id, "status" => 1))->find();
        //针对浙大小乐惠支付，没有userId这个
        if($account){
            $campusCardCard['number2'] =$account;
            $campusCardCard['number'] = $account;
        } else {
            $campusCardCard = M('CampuscardCard')->where(array('campuscard_id' => $campuscard['campuscard_id'], 'user_id' => $user_id))->find();
        }

        //调用接口
        vendor("CampusCard.CampusApi");
        $campusApi = new CampusApi($campuscard);
        //请求参数
        $signData = array(
            "tranamt" => $order['order_amount'] * 100, //金额以分为单位
            "account" => $campusCardCard['number2'],
            "sno" => $campusCardCard['number'],
            "toaccount" => $campuscard['toaccount'],
            "thirdsystem" => $campuscard['thirdsystem'],
            "thirdorderid" => $order['order_sn'],
            "ordertype" => 'phone',
            "orderdesc" => $goodsNameCampusCard,
            "praram1" => $account_id . "||".$pay_type."||" . $order['order_id'] //account_id||订单类型（1:线上商城支付 2:小乐惠订餐支付）||订单编号
        );
        wLog(date('Y-m-d H:i:s') . ' orderPay resCampus xlh campusCardPay: ' . var_export($signData,true), 'ZduRestaurantApi');
        //定义偏移向量
        $iv = array('0', '0', '0', '0', '0', '0', '0', '0');
        //得到签名
        $sign = $campusApi->getSign($signData, $iv);
        if ($sign['status'] == "F") {
            return array('status' => 'F', 'message' => $sign['message']);
        }
        $signData['sign'] = $sign;
        foreach ($signData as $k => $v) {
            $signData[$k] = urlencode($v);
        }
        $interface_ip = $campuscard['interface_ip'];
        $interface_name = "Order/CreateOrder";
        $signData['interface_url'] = $interface_ip . '/' . $interface_name;
        $signData['thirdurl'] = $thirdurl;
        $signData['errorurl'] = $errorurl;
        return array('status' => 'S', 'data' => $signData);
    }

    /**
     * 校园卡支付完成之后同步跳转回来的验证处理操作
     */
    public function checkCampusOrder($dataResonse,$pay_type)
    {
        wLog(date('Y-m-d H:i:s').' checkCampusOrder dataResonse: '.var_export($dataResonse,true),'CampusCard');
        $praram1 = explode("||", $dataResonse['praram1']);
        $accountId = $praram1[0];
        $payClass = $praram1[1]; //模块支付 1:线上商城支付 2：小乐惠订餐支付
        $orderId = $praram1[2];
        if (!$accountId) {
            return array('status' => 'F', 'message' => 'account_id不能为空');
        }
        if (!$orderId) {
            return array('status' => 'F', 'message' => '订单id不能为空');
        }
        if ($payClass != $pay_type) {
            return array('status' => 'F', 'message' => '订单类型错误');
        }
        //查询校园卡支付配置
        $campuscard = M("Campuscard")->where(array("account_id" => $accountId, "status" => 1))->find();
        //调用接口
        vendor("CampusCard.CampusApi");
        $campusApi = new CampusApi($campuscard);
        //定义偏移向量
        $iv = array('0', '0', '0', '0', '0', '0', '0', '0');
        //请求参数
        $signData = array(
            "tranamt" => $dataResonse['tranamt'], //金额需要乘以100，服务端会除以100
            "orderid" => $dataResonse['orderid'],
            "account" => $dataResonse['account'],
            "sno" => $dataResonse['sno'],
            "toaccount" => $dataResonse['toaccount'],
            "thirdsystem" => $dataResonse['thirdsystem'],
            "thirdorderid" => $dataResonse['thirdorderid'],
            "state" => $dataResonse['state'],
            "orderdesc" => $dataResonse['orderdesc'],
            "praram1" => $dataResonse['praram1']
        );
        //得到签名
        $sign = $campusApi->getSign($signData, $iv);
        wLog(date('Y-m-d H:i:s').' checkCampusOrder sign: '.var_export($sign,true),'CampusCard');
        if (isset($sign['status']) && $sign['status'] == "F") {
            return array('status' => 'F', 'message' => $sign['message']);
        }
        if ($sign != urldecode($dataResonse['sign'])) {
            return array('status' => 'F', 'message' => '非法数据');
        }
        //做逻辑处理
        if ($dataResonse['state'] == 1) {
            return array('status' => 'S','order_id'=>$orderId);
        }
    }
}